#include "rn8209d.h"
#include "outputCtl.h"
#include "uuPowerProcess.h"
#include "chargeCtrl.h"

#define			ADSYSCON 		0x00
#define        	ADEMUCON 		0x01
#define        	ADHFConst     	0x02
#define        	ADPStart      	0x03
#define        	ADDStart      	0x04
#define			ADGPQA        	0x05
#define        	ADGPQB        	0x06
#define        	ADPhsA        	0x07
#define        	ADPhsB        	0x08
#define			ADQPHSCAL		0x09
#define			ADAPOSA			0x0a
#define        	ADAPOSB			0x0b
#define        	ADRPOSA 		0x0c
#define        	ADRPOSB 		0x0d
#define        	ADIARMSOS     	0x0e
#define        	ADIBRMSOS     	0x0f
#define        	ADIBGain      	0x10
#define			ADD2FPL       	0x11
#define        	ADD2FPH       	0x12
#define        	ADDCIAH       	0x13
#define        	ADDCIBH       	0x14
#define         ADDCUH		    0x15
#define         ADDCL   	    0x16
#define         ADEMUCON2	    0x17
#define			ADPFCnt    	    0x20
#define        	ADDFcnt    	    0x21
#define        	ADIARMS       	0x22
#define        	ADIBRMS       	0x23
#define        	ADURMS        	0x24
#define			ADUFreq       	0x25
#define        	ADPowerPA     	0x26
#define        	ADPowerPB     	0x27
#define         ADEnergyP   	0x29
#define         ADEnergyP2 	    0x2a
#define         ADEnergyD  	    0x2b
#define         ADEnergyD2    	0x2c
#define         ADEMUStatus   	0x2d
#define         ADSPL_IA      	0x30
#define         ADSPL_IB      	0x31
#define         ADSPL_U       	0x32
#define         ADIE  		    0x40
#define         ADIF  		    0x41
#define         ADRIF    	    0x42
#define         ADSysStatus  	0x43
#define         ADRData      	0x44
#define         ADWData      	0x45
#define         ADDeviceID   	0x7f
#define         WriteEn   	    0xea


#define RN8209_OK            ((uint8_t)0x00)
#define RN8209_ERROR         ((uint8_t)0x01)
#define RN8209_BUSY          ((uint8_t)0x02)
#define RN8209_TIMEOUT		 ((uint8_t)0x03)






#define aGun_NSS_VBUS_Port			GPIOH	
#define aGun_NSS_VBUS_Pin			GPIO_PIN_3 



#define aGun_NSS_VBAT_Port			GPIOH	
#define aGun_NSS_VBAT_Pin			GPIO_PIN_2


#define aGun_RESET_Port				GPIOC	
#define aGun_RESET_Pin				GPIO_PIN_3



#define bGun_NSS_VBUS_Port			GPIOE	
#define bGun_NSS_VBUS_Pin			GPIO_PIN_4


#define bGun_NSS_VBAT_Port			GPIOC
#define bGun_NSS_VBAT_Pin			GPIO_PIN_2


#define bGun_RESET_Port				GPIOH	
#define bGun_RESET_Pin				GPIO_PIN_4


#define __RN8209_DEBUG__			1

typedef enum
{
	GET_VBUS = 0,
	GET_VBAT
}getVolTypeE;




#define SPLITTER_MAX_CURRENT		300				//unit ampere
#define SPLITTER_MAX_VOLTAGE		0.075			//unit volt

uint16_t ry;
uint16_t rx;

double m_voltage;

static void RN8209d_Read(SPI_HandleTypeDef *hspi,getVolTypeE  type, uint8_t tData, uint8_t *rData, uint8_t Size);
static uint8_t RN8209d_Write(SPI_HandleTypeDef *hspi,getVolTypeE  type, uint8_t tData, uint8_t *pData, uint8_t Size);

static void rn8209AutoVoltageCalibration(uint8_t gunId);
	
static void spiDelay(uint16_t n)
{
	for(int j = 0;j < n; j ++)
		for(int i= 0; i < 1000; i++);
}

typedef struct{
	uint32_t deviceId[2];
	uint32_t busVoltage;
	uint32_t batVoltage;
	uint32_t IsoVoltagePG;
	uint32_t IsoVoltageNG;
	uint32_t chargingCurrent;
}rn8209dRegisterDataS;



typedef struct 
{
	uint8_t calibrationVolStatus[2];
	uint8_t calibrationCurStatus[2];
	uint16_t waitCalibration;
	int32_t kValueBusCurGunA;
	int32_t bValueBusCurGunA;
	
	int32_t kValueBusVolGunA;
	int32_t bValueBusVolGunA;
	
	int32_t kValueBatVolGunA;
	int32_t bValueBatVolGunA;
	int32_t kValueBatIsoVolGunA;
	int32_t bValueBatIsoVolGunA;
	
	int32_t kValueBusCurGunB;
	int32_t bValueBusCurGunB;
	
	int32_t kValueBusVolGunB;
	int32_t bValueBusVolGunB;
	
	int32_t kValueBatVolGunB;
	int32_t bValueBatVolGunB;
	int32_t kValueBatIsoVolGunB;
	int32_t bValueBatIsoVolGunB;
}calibrateKbValueS;

typedef struct 
{
	double busVoltageGunA;
	double busCurrentGunA;
	double batVoltageGunA;
	double batInsulationGunA;
	double busVoltageGunB;
	double busCurrentGunB;
	double batVoltageGunB;
	double batInsulationGunB;
}getCurVolValueS;

calibrateKbValueS calibrateKbValue;
getCurVolValueS getCurVolValue;



typedef enum 
{
	AGUNBUSVOLTAG_E = 0,
	AGUNBATCURRENT_E,
	BGUNBUSVOLTAG_E,
	BGUNBATCURRENT_E,
	RN8209EVENTIDMAX
}rn8209Evntid;

typedef  void(*Rn8209dFunptrDef)(SPI_HandleTypeDef *hspi,GPIO_TypeDef  *gpiox,uint16_t pinx,uint32_t *);


typedef struct{
	uint8_t CurrntEvent;	
	uint8_t reserved1;
	uint8_t reserved2;
	uint8_t nextEvent;
	Rn8209dFunptrDef Run8209dPtr;
	
}rn8209dRunProcessS;


rn8209dRegisterDataS  rn8209dRegisterData[2];



static void rn8209DefaultParameter(void)
{
	calibrateKbValue.calibrationVolStatus[GUNA] = 1;
	calibrateKbValue.calibrationVolStatus[GUNB] = 1;
	//write defualt parameter with kb vaule;  
	//connecotr a and connector b bus voltage and batttery valtage;	
	//connecotr a  bus voltage
	calibrateKbValue.kValueBusVolGunA = 991;
	calibrateKbValue.bValueBusVolGunA = 2408;
	//connecotr a batttery valtage;
	calibrateKbValue.kValueBatVolGunA = 988;
	calibrateKbValue.bValueBatVolGunA = -1412;
	//connecotr a insolution voltage  valute 
	calibrateKbValue.kValueBatIsoVolGunA = 953;
	calibrateKbValue.bValueBatIsoVolGunA = 4224;
	
	//connecotr b  bus voltage
	calibrateKbValue.kValueBusVolGunB = 994;
	calibrateKbValue.bValueBusVolGunB = 1279;
	//connecotr b batttery valtage;
	calibrateKbValue.kValueBatVolGunB = 994;
	calibrateKbValue.bValueBatVolGunB = 285;
	//connecotr a  insolution voltage valute 
	calibrateKbValue.kValueBatIsoVolGunB = 958;
	calibrateKbValue.bValueBatIsoVolGunB = 3973;
	
}



void bspRn8209RegInitial(void)
{
	uint8_t i;
	uint8_t j;
	uint8_t countMax;
	uint32_t t_value = 0;
	uint32_t pData[16];
	getVolTypeE  getType[2] = {GET_VBUS,GET_VBAT};
	SPI_HandleTypeDef  getInstace[2] = {hspi1 , hspi4};

	
	HAL_GPIO_WritePin(aGun_RESET_Port, aGun_RESET_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(bGun_RESET_Port, bGun_RESET_Pin, GPIO_PIN_RESET);
	
	HAL_Delay(10);
	
	HAL_GPIO_WritePin(aGun_RESET_Port, aGun_RESET_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(bGun_RESET_Port, bGun_RESET_Pin, GPIO_PIN_SET);

	HAL_Delay(5);
	//set cs pin  push high
	HAL_GPIO_WritePin(aGun_NSS_VBUS_Port, aGun_NSS_VBUS_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(aGun_NSS_VBAT_Port, aGun_NSS_VBAT_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(bGun_NSS_VBUS_Port, bGun_NSS_VBUS_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(bGun_NSS_VBAT_Port, bGun_NSS_VBAT_Pin, GPIO_PIN_SET);	

	HAL_Delay(10);

	
	for(i = 0; i < 2 ; i++)
	{
		for(j = 0; j < 2; j ++)
		{
			countMax = 10;
			do
			{
				//register data parameter write flash address.
				t_value = 0xE5;		////wirte enable bit
				RN8209d_Write(&getInstace[i],getType[j], WriteEn,  (uint8_t*)&t_value, 1);
				t_value = 0x4000;
				RN8209d_Write(&getInstace[i],getType[j],ADSYSCON, (uint8_t*)&t_value, 2);
				t_value = 0x6340;	
				RN8209d_Write(&getInstace[i],getType[j],ADEMUCON, (uint8_t*)&t_value, 2);
				t_value = 0x8000;	
				RN8209d_Write(&getInstace[i],getType[j],ADEMUCON2,(uint8_t*)&t_value, 2);
				t_value = 0x0000;
				RN8209d_Write(&getInstace[i],getType[j],ADDCIAH,(uint8_t*)&t_value, 2);
				t_value = 0x0000;	
				RN8209d_Write(&getInstace[i],getType[j],ADDCIBH,(uint8_t*)&t_value, 2);
				t_value = 0x0000;	
				RN8209d_Write(&getInstace[i],getType[j],ADDCUH,  (uint8_t*)&t_value, 2);
				t_value = 0x0000;	
				RN8209d_Write(&getInstace[i],getType[j],ADDCL,  (uint8_t*)&t_value, 2);
				t_value = 0xDC;	    //save  write data 
				RN8209d_Write(&getInstace[i],getType[j],WriteEn, (uint8_t*)&t_value, 1);
				//read register data 
				RN8209d_Read(&getInstace[i],getType[j],ADDeviceID,(uint8_t*)&pData[0], 3);
				RN8209d_Read(&getInstace[i],getType[j],ADSYSCON,(uint8_t*)&pData[1], 2);
				RN8209d_Read(&getInstace[i],getType[j],ADEMUCON,(uint8_t*)&pData[2], 2);
				RN8209d_Read(&getInstace[i],getType[j],ADEMUCON2,(uint8_t*)&pData[3], 2);
				#if defined (__RN8209_DEBUG__)
				printf("Rn8209 register ADDeviceID=0x%02x,ADSYSCON=0x%02x ,ADEMUCON=0x%02x,ADEMUCON2=0x%02x\t\n",pData[0],pData[1],pData[2],pData[3]);
				#endif				
				
				//register data verification 
				if(((pData[0]& 0x00ffffff)  == 0x00000982) && ((pData[1] & 0xffff) == 0x4000) && ((pData[2]& 0xffff)  == 0x6340) && ((pData[3]& 0xffff)  == 0x8000))
				{
					break;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Rn8209 register ADDeviceID=0x%02x,ADSYSCON=%02x ,ADEMUCON=0x%02x,ADEMUCON2=0x%02x\t\n",pData[0],pData[1],pData[2],pData[3]);
					#endif
				}
				if(countMax == 1)
				{
					#if defined (__RN8209_DEBUG__)
					printf("Rn8209 init  failed\t\n");
					#endif
					return ;
				}
			}while(countMax --);
		}
	}
	calibrateKbValue.waitCalibration = 500;
	#if defined (__RN8209_DEBUG__)
	printf("Rn8209 init succesed \t\n");
	#endif
	rn8209DefaultParameter();
}

static void RN8209d_Read(SPI_HandleTypeDef *hspi,getVolTypeE  type,uint8_t tData, uint8_t *rData, uint8_t Size)
{
	if(hspi->Instance == SPI1)
	{
		//read battery  voltage
		if(GET_VBUS == type)
		{
			HAL_GPIO_WritePin(aGun_NSS_VBUS_Port, aGun_NSS_VBUS_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			HAL_SPI_Transmit(&hspi1, &tData, 1, HAL_MAX_DELAY);		
			for(uint8_t i = 0; i < Size; i ++)
			{
				HAL_SPI_Receive(&hspi1,&rData[i],1,HAL_MAX_DELAY); 
			}		
			spiDelay(1);
			HAL_GPIO_WritePin(aGun_NSS_VBUS_Port, aGun_NSS_VBUS_Pin, GPIO_PIN_SET);
		}
		//read bus  voltage
		else if(GET_VBAT == type)
		{
			HAL_GPIO_WritePin(aGun_NSS_VBAT_Port, aGun_NSS_VBAT_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			HAL_SPI_Transmit(&hspi1, &tData, 1, HAL_MAX_DELAY);
			for(uint8_t i = 0; i < Size; i ++)
			{
				HAL_SPI_Receive(&hspi1,&rData[i],1,HAL_MAX_DELAY);
			}
			spiDelay(1);
			HAL_GPIO_WritePin(aGun_NSS_VBAT_Port, aGun_NSS_VBAT_Pin, GPIO_PIN_SET);
		}
	}
	if(hspi->Instance == SPI4)
	{
		//read battery  voltage
		if(GET_VBUS == type)
		{
			HAL_GPIO_WritePin(bGun_NSS_VBUS_Port, bGun_NSS_VBUS_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			HAL_SPI_Transmit(&hspi4, &tData, 1, HAL_MAX_DELAY);
				
			for(uint8_t i = 0; i < Size; i ++)
			{
				HAL_SPI_Receive(&hspi4,&rData[i],1,HAL_MAX_DELAY); 
			}
		
			spiDelay(1);
			HAL_GPIO_WritePin(bGun_NSS_VBUS_Port, bGun_NSS_VBUS_Pin, GPIO_PIN_SET);
		}		
		//read bus  voltage
		else if(GET_VBAT == type)
		{
			HAL_GPIO_WritePin(bGun_NSS_VBAT_Port, bGun_NSS_VBAT_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			HAL_SPI_Transmit(&hspi4, &tData, 1, HAL_MAX_DELAY);
				
			for(uint8_t i = 0; i < Size; i ++)
			{
				HAL_SPI_Receive(&hspi4,&rData[i],1,HAL_MAX_DELAY); 
			}
		
			spiDelay(1);
			HAL_GPIO_WritePin(bGun_NSS_VBAT_Port, bGun_NSS_VBAT_Pin, GPIO_PIN_SET);
		}
	}
}

static uint8_t RN8209d_Write(SPI_HandleTypeDef *hspi,getVolTypeE  type, uint8_t tData, uint8_t *pData, uint8_t Size)
{
	uint8_t reg;
	HAL_StatusTypeDef spiStatus;
	if(hspi->Instance == SPI1)
	{
		if(GET_VBUS == type)
		{
			HAL_GPIO_WritePin(aGun_NSS_VBUS_Port, aGun_NSS_VBUS_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			reg = tData | 0x80;
			spiStatus = HAL_SPI_Transmit(&hspi1, &reg, 1, HAL_MAX_DELAY);
			if(spiStatus != HAL_OK)
			{
				return 1;
			}
			for(uint8_t i = 0; i < Size; i ++)
			{
				spiStatus = HAL_SPI_Transmit(&hspi1, &pData[i], 1, HAL_MAX_DELAY); 
				if(spiStatus != HAL_OK)
				{
					return 1;
				}
			}
			spiDelay(1);
			HAL_GPIO_WritePin(aGun_NSS_VBUS_Port, aGun_NSS_VBUS_Pin, GPIO_PIN_SET);
		}
		else if(GET_VBAT == type)
		{
			HAL_GPIO_WritePin(aGun_NSS_VBAT_Port, aGun_NSS_VBAT_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			reg = tData | 0x80;
			spiStatus = HAL_SPI_Transmit(&hspi1, &reg, 1, HAL_MAX_DELAY);
			
			if(spiStatus != HAL_OK)
			{
				return 1;
			}
			for(uint8_t i = 0; i < Size; i ++)
			{
				spiStatus = HAL_SPI_Transmit(&hspi1, &pData[i], 1, HAL_MAX_DELAY); 
				
				if(spiStatus != HAL_OK)
				{
					return 1;
				}
			}
			spiDelay(1);
			HAL_GPIO_WritePin(aGun_NSS_VBAT_Port, aGun_NSS_VBAT_Pin, GPIO_PIN_SET);
		}
	}
	if(hspi->Instance == SPI4)
	{		
		if(GET_VBUS == type)
		{
			HAL_GPIO_WritePin(bGun_NSS_VBUS_Port, bGun_NSS_VBUS_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			reg = tData | 0x80;
			spiStatus = HAL_SPI_Transmit(&hspi4, &reg, 1, HAL_MAX_DELAY);
			
			if(spiStatus != HAL_OK)
			{
				return 1;
			}
			for(uint8_t i = 0; i < Size; i ++)
			{
				spiStatus = HAL_SPI_Transmit(&hspi4, &pData[i], 1, HAL_MAX_DELAY); 
				
				if(spiStatus != HAL_OK)
				{
					return 1;
				}
			}
			spiDelay(1);
			HAL_GPIO_WritePin(bGun_NSS_VBUS_Port, bGun_NSS_VBUS_Pin, GPIO_PIN_SET);		
		}
		else if(GET_VBAT == type)
		{
			HAL_GPIO_WritePin(bGun_NSS_VBAT_Port, bGun_NSS_VBAT_Pin, GPIO_PIN_RESET);
			spiDelay(1);
			reg = tData | 0x80;
			spiStatus = HAL_SPI_Transmit(&hspi4, &reg, 1, HAL_MAX_DELAY);
			
			if(spiStatus != HAL_OK)
			{
				return 1;
			}
			for(uint8_t i = 0; i < Size; i ++)
			{
				spiStatus = HAL_SPI_Transmit(&hspi4, &pData[i], 1, HAL_MAX_DELAY); 
				
				if(spiStatus != HAL_OK)
				{
					return 1;
				}
			}
			spiDelay(1);
			HAL_GPIO_WritePin(bGun_NSS_VBAT_Port, bGun_NSS_VBAT_Pin, GPIO_PIN_SET);
		}
	}
	return 0;
}

// Get rn8209 first output current .
static void getRn8209Iarms(SPI_HandleTypeDef *hspi,getVolTypeE  type,uint32_t* getValue)
{
	uint8_t t_value[4] = {0x00};
	RN8209d_Read(hspi,type, ADIARMS, (uint8_t*)t_value, 3);	
	*getValue = (uint32_t)t_value[0]<<16 | t_value[1]<<8 | t_value[2];
}

// Get rn8209 second output current .
static void getRn8209Ibrms(SPI_HandleTypeDef *hspi,getVolTypeE  type,uint32_t* getValue)
{
	uint8_t t_value[4] = {0x00};
	RN8209d_Read(hspi,type, ADIBRMS, (uint8_t*)t_value, 3);	
	*getValue = (uint32_t)t_value[0]<<16 | t_value[1]<<8 | t_value[2];
}

// Get rn8209 third output voltage .
static void getRn8209Urms(SPI_HandleTypeDef *hspi,getVolTypeE  type,uint32_t* getValue)
{
	uint8_t t_value[4] = {0x00};
	RN8209d_Read(hspi,type, ADURMS, (uint8_t*)t_value, 3);	
	*getValue = (uint32_t)t_value[0]<<16 | t_value[1]<<8 | t_value[2];
}


void rn8209AutoCurrentCalibration(uint8_t gunId)
{
	//current sample current non condition
}


//The K B value is rounded to third decimal places and magnified by 100 times.
static void rn8209AutoCalibrationConnectorA(void)
{
	static uint8_t calibrationStep = 0;
	static uint16_t sampleCount = 0;
	static uint32_t waitcalibrationStatus = 0;
	
	static uint32_t busCalibrationVoltage[2];
	static uint32_t batCalibrationVoltage[2];
	static uint32_t visoCalibrationVoltage[2];	
				
	static uint32_t systickBak;
	uint32_t timeoutMs = 10000; //unit ms
	uint32_t currTick ;
	uint32_t voltageTemp;
	uint32_t setVoltageGroup[2] = {400,750};
	
	if(calibrateKbValue.calibrationVolStatus[GUNA] == 1)
	{
		return ;
	}
	//close AC contactor 
	setKeyValueHandle(AC_CONTROL_MODULE,1,0,0x00,0x00,NULL);
	//close A gun and B gun DC contactor
	setKeyValueHandle(A_DC_CONTACTOR1_MODULE,1,0,0x00,0x00,NULL);
	if(calibrationStep == 0)
	{	
		// set power module voltage 400V		
		setModuleCtrl(GUNA,1,POWER_ON|SET_VI,setVoltageGroup[0]*10,10);
		systickBak  = HAL_GetTick();
		calibrationStep = 1;
	}	
	else if(calibrationStep == 1)
	{		
		
		setModuleCtrl(GUNA,1,POWER_ON|SET_VI,setVoltageGroup[0]*10,10);
		currTick = HAL_GetTick();		
		if (currTick >= systickBak)    // systickBak uint32 
		{
			if(currTick - systickBak > timeoutMs)
			{
				getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
				busCalibrationVoltage[0] = (double)voltageTemp * Rbus  / RD * 1.2 / 10000;
				//when voltage greate than 390 volt that the circuit sample data.
				if(busCalibrationVoltage[0] >  setVoltageGroup[0] - 10)
				{
						//calibration bus voltage 
						getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
						busCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
						
						//calibration battery voltage
						getRn8209Ibrms(&hspi1,GET_VBAT,&voltageTemp);	
						batCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
						
						//calibration insolution voltage
						getRn8209Urms(&hspi1,GET_VBAT,&voltageTemp);	
						visoCalibrationVoltage[0] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
						sampleCount = 0;	
						calibrationStep = 2;
						waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
		else
		{
			// currTick 
			if(currTick + (~systickBak) > timeoutMs)
			{
				getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
				busCalibrationVoltage[0] = (double)voltageTemp * Rbus  / RD * 1.2 / 10000;		
				//when voltage greate than 390 volt that the circuit sample data.
				if(busCalibrationVoltage[0] >  setVoltageGroup[0] - 10)
				{
					//calibration bus voltage 
					getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
					busCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration battery voltage
					getRn8209Ibrms(&hspi1,GET_VBAT,&voltageTemp);	
					batCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration insolution voltage
					getRn8209Urms(&hspi1,GET_VBAT,&voltageTemp);	
					visoCalibrationVoltage[0] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
					sampleCount = 0;	
					calibrationStep = 2;
					waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
	}
	else if(calibrationStep == 2)
	{
		setModuleCtrl(GUNA,1,POWER_ON|SET_VI,setVoltageGroup[1]*10,10);
		systickBak = HAL_GetTick();
		calibrationStep = 3;
	}
	else if(calibrationStep == 3)
	{
		setModuleCtrl(GUNA,1,POWER_ON|SET_VI,setVoltageGroup[1]*10,10);
		currTick = HAL_GetTick();
		if (currTick >= systickBak)    // systickBak uint32 
		{
			if(currTick - systickBak > timeoutMs)
			{
				// set power module voltage 750V	
				getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);	
				busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10;
				
				//when voltage greate than 750 volt that the circuit sample data.
				if(busCalibrationVoltage[1] >  setVoltageGroup[1] - 10)
				{
					//calibration bus voltage 
					getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
					busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration battery voltage
					getRn8209Ibrms(&hspi1,GET_VBAT,&voltageTemp);	
					batCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration insolution voltage
					getRn8209Urms(&hspi1,GET_VBAT,&voltageTemp);	
					visoCalibrationVoltage[1] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
					calibrationStep = 4;
					waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
		else
		{
			// currTick 
			if(currTick + (~systickBak) > timeoutMs)
			{
				// set power module voltage 750V		
				getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);	
				busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10;
				
				//when voltage greate than 750 volt that the circuit sample data.
				if(busCalibrationVoltage[1] >  setVoltageGroup[1] - 10)
				{
					//calibration bus voltage 
					getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
					busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration battery voltage
					getRn8209Ibrms(&hspi1,GET_VBAT,&voltageTemp);	
					batCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration insolution voltage
					getRn8209Urms(&hspi1,GET_VBAT,&voltageTemp);	
					visoCalibrationVoltage[1] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
					sampleCount = 0;	
					calibrationStep = 4;
					waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
	}
	else if(calibrationStep == 4)
	{
		//start calculate    formula y=kx+b;
		//k = (y2 - y1)/(x2 - x1);
		//b = y1 - (y2 - y1)/(x2 - x1) * x1;
		//get bus battery voltage  K  B value;
		//(float)(batCalibrationVoltage[0] - (batCalibrationVoltage[1] - batCalibrationVoltage[0])/(setVoltageGroup[1] - setVoltageGroup[0]) * setVoltageGroup[0]);
		//setVoltageGroup[1] - (setVoltageGroup[1] - setVoltageGroup[0])/(batCalibrationVoltage[1] - batCalibrationVoltage[0]) * batCalibrationVoltage[0]
		
		setModuleCtrl(GUNA,1,POWER_DOWN,0,0);
		calibrateKbValue.kValueBusVolGunA =  ((setVoltageGroup[1] - setVoltageGroup[0])*1000 /(busCalibrationVoltage[1] - busCalibrationVoltage[0]));
		calibrateKbValue.bValueBusVolGunA = setVoltageGroup[0] * 1000 - ((setVoltageGroup[1] - setVoltageGroup[0])*1000 * busCalibrationVoltage[0])/(busCalibrationVoltage[1] - busCalibrationVoltage[0]);
		calibrateKbValue.kValueBatVolGunA = 	((setVoltageGroup[1] - setVoltageGroup[0])* 1000/(batCalibrationVoltage[1] - batCalibrationVoltage[0]));
		calibrateKbValue.bValueBatVolGunA = setVoltageGroup[0]* 1000 - ((setVoltageGroup[1] - setVoltageGroup[0]) * 1000 * batCalibrationVoltage[0])/(batCalibrationVoltage[1] - batCalibrationVoltage[0]) ;
		calibrateKbValue.kValueBatIsoVolGunA = ((setVoltageGroup[1] - setVoltageGroup[0])* 1000/(visoCalibrationVoltage[1] - visoCalibrationVoltage[0]));
		calibrateKbValue.bValueBatIsoVolGunA = setVoltageGroup[0]* 1000 - ((setVoltageGroup[1] - setVoltageGroup[0]) *1000 * visoCalibrationVoltage[0])/(visoCalibrationVoltage[1] - visoCalibrationVoltage[0]);
		calibrationStep = 0;
		calibrateKbValue.calibrationVolStatus[GUNA]  = 1;
		
		#if defined (__RN8209_DEBUG__)
		printf("kValueBusVolGunA = %d \t\n",calibrateKbValue.kValueBusVolGunA);
		printf("bValueBusVolGunA = %d \t\n",calibrateKbValue.bValueBusVolGunA);
		printf("kValueBatVolGunA = %d \t\n",calibrateKbValue.kValueBatVolGunA);
		printf("bValueBatVolGunA = %d \t\n",calibrateKbValue.bValueBatVolGunA);
		printf("kValueBatIsoVolGunA = %d \t\n",calibrateKbValue.kValueBatIsoVolGunA);
		printf("bValueBatIsoVolGunA = %d \t\n",calibrateKbValue.bValueBatIsoVolGunA);
		#endif
	}
}




static void rn8209AutoCalibrationConnectorB(void)
{
	static uint8_t calibrationStep = 0;
	static uint16_t sampleCount = 0;
	static uint32_t waitcalibrationStatus = 0;
	
	static uint32_t busCalibrationVoltage[2];
	static uint32_t batCalibrationVoltage[2];
	static uint32_t visoCalibrationVoltage[2];	
				
	static uint32_t systickBak;
	uint32_t timeoutMs = 8000; //unit ms
	uint32_t currTick ;
	uint32_t voltageTemp;
	uint32_t setVoltageGroup[2] = {400,750};
	if(calibrateKbValue.calibrationVolStatus[GUNB] == 1)
	{
		return ;
	}
	//close AC contactor 
	setKeyValueHandle(AC_CONTROL_MODULE,1,0,0x00,0x00,NULL);
	//close A gun and B gun DC contactor
	setKeyValueHandle(B_DC_CONTACTOR1_MODULE,1,0,0x00,0x00,NULL);

	if(calibrationStep == 0)
	{	
		// set power module voltage 400V		
		setModuleCtrl(GUNB,1,POWER_ON|SET_VI,setVoltageGroup[0]*10,10);
		systickBak  = HAL_GetTick();
		calibrationStep = 1;
	}	
	else if(calibrationStep == 1)
	{
		setModuleCtrl(GUNB,1,POWER_ON|SET_VI,setVoltageGroup[0]*10,10);
		currTick = HAL_GetTick();
		
		if (currTick >= systickBak)    // systickBak uint32 
		{
			if(currTick - systickBak > timeoutMs)
			{
				getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
				busCalibrationVoltage[0] = (double)voltageTemp * Rbus  / RD * 1.2 / 10000;
				//when voltage greate than 390 volt that the circuit sample data.
				if(busCalibrationVoltage[0] >  setVoltageGroup[0] - 10)
				{
						//calibration bus voltage 
						getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
						busCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
						
						//calibration battery voltage
						getRn8209Ibrms(&hspi4,GET_VBAT,&voltageTemp);	
						batCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
						
						//calibration insolution voltage
						getRn8209Urms(&hspi4,GET_VBAT,&voltageTemp);	
						visoCalibrationVoltage[0] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
						sampleCount = 0;	
						calibrationStep = 2;
						waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
		else
		{
			// currTick 
			if(currTick + (~systickBak) > timeoutMs)
			{
				getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
				busCalibrationVoltage[0] = (double)voltageTemp * Rbus  / RD * 1.2 / 10000;
				//when voltage greate than 390 volt that the circuit sample data.
				if(busCalibrationVoltage[0] >  setVoltageGroup[0] - 10)
				{
					//calibration bus voltage 
					getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
					busCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration battery voltage
					getRn8209Ibrms(&hspi4,GET_VBAT,&voltageTemp);	
					batCalibrationVoltage[0] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration insolution voltage
					getRn8209Urms(&hspi4,GET_VBAT,&voltageTemp);	
					visoCalibrationVoltage[0] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
					sampleCount = 0;	
					calibrationStep = 2;
					waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
	}
	else if(calibrationStep == 2)
	{
		setModuleCtrl(GUNB,1,POWER_ON|SET_VI,setVoltageGroup[1]*10,10);
		systickBak = HAL_GetTick();
		calibrationStep = 3;
	}
	else if(calibrationStep == 3)
	{
		setModuleCtrl(GUNB,1,POWER_ON|SET_VI,setVoltageGroup[1]*10,10);
		currTick = HAL_GetTick();
		if (currTick >= systickBak)    // systickBak uint32 
		{
			if(currTick - systickBak > timeoutMs)
			{
				// set power module voltage 750V	
				getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);	
				busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10;
				
				//when voltage greate than 750 volt that the circuit sample data.
				if(busCalibrationVoltage[1] >  setVoltageGroup[1] - 10)
				{
					//calibration bus voltage 
					getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
					busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration battery voltage
					getRn8209Ibrms(&hspi4,GET_VBAT,&voltageTemp);	
					batCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration insolution voltage
					getRn8209Urms(&hspi4,GET_VBAT,&voltageTemp);	
					visoCalibrationVoltage[1] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
					calibrationStep = 4;
					waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
		else
		{
			// currTick 
			if(currTick + (~systickBak) > timeoutMs)
			{
				// set power module voltage 750V		
				getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);	
				busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10;
				
				//when voltage greate than 750 volt that the circuit sample data.
				if(busCalibrationVoltage[1] >  setVoltageGroup[1] - 10)
				{
					//calibration bus voltage 
					getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
					busCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration battery voltage
					getRn8209Ibrms(&hspi4,GET_VBAT,&voltageTemp);	
					batCalibrationVoltage[1] = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
					
					//calibration insolution voltage
					getRn8209Urms(&hspi4,GET_VBAT,&voltageTemp);	
					visoCalibrationVoltage[1] = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 
					sampleCount = 0;	
					calibrationStep = 4;
					waitcalibrationStatus = 0;
				}
				else
				{
					#if defined (__RN8209_DEBUG__)
					printf("Start power module timeout\n");
					#endif
				}
			}
		}
	}
	else if(calibrationStep == 4)
	{
		
		//start calculate    formula y=kx+b;
		//k = (y2 - y1)/(x2 - x1);
		//b = y1 - (y2 - y1)/(x2 - x1) * x1;
		//get bus battery voltage  K  B value;
		//(float)(batCalibrationVoltage[0] - (batCalibrationVoltage[1] - batCalibrationVoltage[0])/(setVoltageGroup[1] - setVoltageGroup[0]) * setVoltageGroup[0]);
		//setVoltageGroup[1] - (setVoltageGroup[1] - setVoltageGroup[0])/(batCalibrationVoltage[1] - batCalibrationVoltage[0]) * batCalibrationVoltage[0]
		
		setModuleCtrl(GUNB,1,POWER_DOWN,0,0);
		calibrateKbValue.kValueBusVolGunB =  ((setVoltageGroup[1] - setVoltageGroup[0])*1000 /(busCalibrationVoltage[1] - busCalibrationVoltage[0]));
		calibrateKbValue.bValueBusVolGunB = setVoltageGroup[0] * 1000 - ((setVoltageGroup[1] - setVoltageGroup[0])*1000 * busCalibrationVoltage[0])/(busCalibrationVoltage[1] - busCalibrationVoltage[0]);
		calibrateKbValue.kValueBatVolGunB = 	((setVoltageGroup[1] - setVoltageGroup[0])* 1000/(batCalibrationVoltage[1] - batCalibrationVoltage[0]));
		calibrateKbValue.bValueBatVolGunB = setVoltageGroup[0]* 1000 - ((setVoltageGroup[1] - setVoltageGroup[0]) * 1000 * batCalibrationVoltage[0])/(batCalibrationVoltage[1] - batCalibrationVoltage[0]) ;
		calibrateKbValue.kValueBatIsoVolGunB = ((setVoltageGroup[1] - setVoltageGroup[0])* 1000/(visoCalibrationVoltage[1] - visoCalibrationVoltage[0]));
		calibrateKbValue.bValueBatIsoVolGunB = setVoltageGroup[0]* 1000 - ((setVoltageGroup[1] - setVoltageGroup[0]) *1000 * visoCalibrationVoltage[0])/(visoCalibrationVoltage[1] - visoCalibrationVoltage[0]);
		calibrationStep = 0;
		calibrateKbValue.calibrationVolStatus[GUNB]  = 1;
		
		#if defined (__RN8209_DEBUG__)
		printf("kValueBusVolGunB = %d \t\n",calibrateKbValue.kValueBusVolGunB);
		printf("bValueBusVolGunB = %d \t\n",calibrateKbValue.bValueBusVolGunB);
		printf("kValueBatVolGunB = %d \t\n",calibrateKbValue.kValueBatVolGunB);
		printf("bValueBatVolGunB = %d \t\n",calibrateKbValue.bValueBatVolGunB);
		printf("kValueBatIsoVolGunB = %d \t\n",calibrateKbValue.kValueBatIsoVolGunB);
		printf("bValueBatIsoVolGunB = %d \t\n",calibrateKbValue.bValueBatIsoVolGunB);
		#endif
	}
}



uint8_t checkVolCalibrationStatus(uint8_t gunId)
{
	if(gunId >= GUNNUM_MAX)
	{
		return 0;
	}
	if(gunId == GUNA)
	{
		return calibrateKbValue.calibrationVolStatus[GUNA];
	}
	else if(gunId == GUNB)
	{
		return calibrateKbValue.calibrationVolStatus[GUNB];
	}
	return 0;
}


uint8_t checkCurCalibrationStatus(uint8_t gunId)
{
	if(gunId >= GUNNUM_MAX)
	{
		return 0;
	}
	if(gunId == GUNA)
	{
		return calibrateKbValue.calibrationCurStatus[GUNA];
	}
	else if(gunId == GUNB)
	{
		return calibrateKbValue.calibrationCurStatus[GUNB];
	}
}


//Amplify the current value by 1000 times.  unit  mA
uint32_t getAgunBusCurrent(void)
{
	uint32_t busCurrent;
	uint32_t  currentTemp;
	uint32_t  rtlcurrent;
	getRn8209Iarms(&hspi1,GET_VBUS,&currentTemp);
	busCurrent = currentTemp * 	calibrateKbValue.kValueBusCurGunA + calibrateKbValue.bValueBusCurGunA;
	return busCurrent;
}




//Amplify the current value by 1000 times.  unit  mV
uint32_t getAgunBusVoltage(void)
{
	uint32_t busVoltage;
	uint32_t  voltageTemp;
	uint32_t  rtlVoltage;
	getRn8209Urms(&hspi1,GET_VBUS,&voltageTemp);
	busVoltage = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
	rtlVoltage = busVoltage * 	calibrateKbValue.kValueBusVolGunA + calibrateKbValue.bValueBusVolGunA;
	rtlVoltage = rtlVoltage / 100;  //Accuracy of 0.1V    0.1 unit;
	return rtlVoltage;	
}
//Amplify the current value by 1000 times.  unit  mV
uint32_t getAgunBatVoltage(void)
{
	
	uint32_t batVoltage;
	uint32_t  voltageTemp;
	uint32_t  rtlVoltage;
	getRn8209Ibrms(&hspi1,GET_VBAT,&voltageTemp);
	batVoltage = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
	rtlVoltage = batVoltage * 	calibrateKbValue.kValueBatVolGunA + calibrateKbValue.bValueBatVolGunA;
	rtlVoltage = rtlVoltage / 100;  //Accuracy of 0.1V    0.1 unit;
	return rtlVoltage;	
}

//Amplify the current value by 1000 times.  unit  mV
uint32_t getAgunBatIsoVoltage(void)
{
	uint32_t  voltageTemp;
	uint32_t  rtlVoltage;
	uint32_t visoVoltage;
	getRn8209Urms(&hspi1,GET_VBAT,&voltageTemp);
	visoVoltage = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 	
	rtlVoltage = visoVoltage * calibrateKbValue.kValueBatIsoVolGunA + calibrateKbValue.bValueBatIsoVolGunA;
	rtlVoltage = rtlVoltage / 100;  //Accuracy of 0.1V    0.1 unit;
	return rtlVoltage;	
}



//Amplify the current value by 1000 times.  unit  mA
uint32_t getBgunBusCurrent(void)
{
	uint32_t busCurrent;
	uint32_t  currentTemp;
	uint32_t  rtlcurrent;
	getRn8209Iarms(&hspi4,GET_VBUS,&currentTemp);
	busCurrent = currentTemp * calibrateKbValue.kValueBusCurGunB + calibrateKbValue.bValueBusCurGunB;
	return busCurrent;
}

//Amplify the current value by 1000 times.  unit  mV
uint32_t getBgunBusVoltage(void)
{
	uint32_t busVoltage;
	uint32_t  voltageTemp;
	uint32_t  rtlVoltage;
	getRn8209Urms(&hspi4,GET_VBUS,&voltageTemp);
	busVoltage = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
	rtlVoltage = busVoltage * calibrateKbValue.kValueBusVolGunB + calibrateKbValue.bValueBusVolGunB;
	rtlVoltage = rtlVoltage / 100;  //Accuracy of 0.1V    0.1v  unit;
	return rtlVoltage;
}


//Amplify the current value by 1000 times.  unit  mV
uint32_t getBgunBatVoltage(void)
{
	uint32_t batVoltage;
	uint32_t voltageTemp;
	uint32_t rtlVoltage;
	uint32_t batCalibrationVoltage;
	getRn8209Ibrms(&hspi4,GET_VBAT,&voltageTemp);
	batVoltage = (double)voltageTemp * Rbus / RD * 1.2 / 10000;	
	rtlVoltage = batVoltage * calibrateKbValue.kValueBatVolGunB + calibrateKbValue.bValueBatVolGunB;
	rtlVoltage = rtlVoltage / 100;  //Accuracy of 0.1V    0.1v  unit;
	return rtlVoltage;	
}


//Amplify the current value by 1000 times.  unit  mV
uint32_t getBgunBatIsoVoltage(void)
{
	uint32_t visoVoltage;
	uint32_t  voltageTemp;
	uint32_t  rtlVoltage;
	getRn8209Urms(&hspi4,GET_VBAT,&voltageTemp);
	visoVoltage = (double)voltageTemp * Rbat / RD * 1.2 / 10000; 	
	rtlVoltage = visoVoltage * calibrateKbValue.kValueBatIsoVolGunB + calibrateKbValue.bValueBatIsoVolGunB;
	rtlVoltage = rtlVoltage / 100;  //Accuracy of 0.1V    0.1v unit;
	return rtlVoltage;	
}


//void ISO_Calculate(void)
//{ 
//    float  viso1;
//    float viso2;
//    float temp = 0.000000;
//    float temp_v1 = 0.000000;
//    
//	m_voltage = (float)rn8209Vlaue[0]/1000; //
//    
//	//m_voltage = (float)p->AD_Temp[0]/10; //
//    
//    viso1 = (float)rn8209Vlaue[0]/ 10000;
//    viso2 = (float)rn8209Vlaue[0] / 10000;

//   
//    temp =   (m_voltage*RB*RC*RD*(RA+RD)*(viso1-viso2));
//	
//    temp_v1 = ((RA+RD)*(m_voltage*RD*viso2*RB-viso1*viso2*RB*RD -viso1*RA*viso2*RB)+m_voltage*RB*RC*RD*(viso2-viso1));
//    ry = temp / temp_v1;
//    
//    temp = ((m_voltage*RB*RC*RD)*(viso2-viso1));
//	
//    temp_v1 = (m_voltage*RC*RD*(viso1-viso2)-viso1*viso2*RB*(RA+RD));
//    
//    rx = temp / temp_v1;
//}
//	



float ABUSVoltage = 0.00;
float ABATVoltage = 0.00;
float AISOVoltage = 0.00;


float BBUSVoltage = 0.00;
float BBATVoltage = 0.00;
float BISOVoltage = 0.00;

float AbatBusVoltage = 0.00;
float BbatBusVoltage = 0.00;
uint32_t rn8209Vlaue[8];



void rn8209LogicEntry(void)
{
	

	uint32_t AconnectorBusVol;
	uint32_t AconnectorbatVol;
	uint32_t AconnectorisoVol[2];
	uint32_t BconnectorBusVol;
	uint32_t BconnectorbatVol;
	uint32_t BconnectorisoVol[2];
	
	
	rn8209AutoCalibrationConnectorA();
	rn8209AutoCalibrationConnectorB();
	
	
	if(calibrateKbValue.calibrationVolStatus[GUNA]  == 1 && calibrateKbValue.calibrationVolStatus[GUNA]  == 1 )
	{
		setACContactor(1);
		setModuleCtrl(GUNA,1,POWER_ON|SET_VI,900*10,10);
		setModuleCtrl(GUNB,1,POWER_ON|SET_VI,800*10,10);
		
		AconnectorBusVol = getAgunBusVoltage();
		
		AconnectorbatVol = getAgunBatVoltage();
		
		AconnectorisoVol[0] = getAgunBatIsoVoltage();
		
		AconnectorisoVol[1] = getAgunBatIsoVoltage();
		
		
		printf("A bus voltage = %d, bat voltage = %d, pg voltage = %d, ng voltage = %d\n", AconnectorBusVol,AconnectorbatVol,AconnectorisoVol[0],AconnectorisoVol[1]);
		
		
		BconnectorBusVol = getBgunBusVoltage();
		
		BconnectorbatVol = getBgunBatVoltage();
		
		BconnectorisoVol[0] = getBgunBatIsoVoltage();
		
		BconnectorisoVol[1] = getBgunBatIsoVoltage();
		
		printf("B bus voltage = %d, bat voltage = %d, pg voltage = %d, ng voltage = %d\n", BconnectorBusVol,BconnectorbatVol,BconnectorisoVol[0],BconnectorisoVol[1]);
	}
		
//		setACContactor(1);
//		setModuleCtrl(GUNA,1,POWER_ON|SET_VI,400*10,10);
//		setModuleCtrl(GUNB,1,POWER_ON|SET_VI,400*10,10);
//		
//			
//		getRn8209Urms(&hspi1,GET_VBUS,&rn8209Vlaue[0]);
//		getRn8209Ibrms(&hspi1,GET_VBAT,&rn8209Vlaue[1]);	
//		getRn8209Urms(&hspi1,GET_VBAT,&rn8209Vlaue[2]);
//		
//		ABUSVoltage = rn8209Vlaue[0] * 1.800910 / 910 * 1.2;	
//		ABATVoltage = rn8209Vlaue[1] * 1.800910 / 910 * 1.2;
//		AISOVoltage = rn8209Vlaue[2] * 1.500910 / 910 * 1.2;
//		
//		printf(" A ABUSVoltage : %2.2f, ABATVoltage %2.2f , AISOVoltage %2.2f\n",ABUSVoltage,ABATVoltage,AISOVoltage);
//		
//		getRn8209Urms(&hspi4,GET_VBUS,&rn8209Vlaue[3]);
//		getRn8209Ibrms(&hspi4,GET_VBAT,&rn8209Vlaue[4]);	
//		getRn8209Urms(&hspi4,GET_VBAT,&rn8209Vlaue[5]);
//		
//		BBUSVoltage = rn8209Vlaue[3] * 1.800910 / 910 * 1.2;	
//		BBATVoltage = rn8209Vlaue[4] * 1.800910 / 910 * 1.2;	
//		BISOVoltage = rn8209Vlaue[5] * 1.500910 / 910 * 1.2;
//		
//		printf(" B  BISOVoltage : %2.2f, BBATVoltage %2.2f , BISOVoltage %2.2f\n",BBUSVoltage,BBATVoltage,BISOVoltage);

//		
//		
//		
//		getRn8209Iarms(&hspi1,GET_VBAT,&rn8209Vlaue[2]);	
//		getRn8209Urms(&hspi1,GET_VBAT,&rn8209Vlaue[1]);
//		
//		
//		getRn8209Iarms(&hspi4,GET_VBUS,&rn8209Vlaue[1]);
//		getRn8209Urms(&hspi4,GET_VBUS,&rn8209Vlaue[1]);
//		
//		getRn8209Ibrms(&hspi4,GET_VBAT,&rn8209Vlaue[2]);	
//		getRn8209Urms(&hspi4,GET_VBAT,&rn8209Vlaue[1]);
//		
//		AbatBusVoltage = rn8209Vlaue[1] * 1.800910 / 910 * 1.2;	
//		BbatBusVoltage = rn8209Vlaue[3] * 1.800910 / 910 * 1.2;
//		
//		printf(" Charge A  voltage : %2.2f,   Charge B voltage  current   %2.2f\n",AbatBusVoltage,BbatBusVoltage);
//	}
}








